//
//  PackDetailController.m
//  MyTrip
//
//  Created by zhang on 15/8/27.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "PackDetailController.h"
#import "AsyncPostURLConnection.h"
#import "Pack.h"
#import "LabelCell.h"
#import "UIImageView+WebCache.h"
#import "MyTextCell.h"
#import "DKNightVersion.h"
#import "MBProgressHUD.h"

@interface PackDetailController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) Pack *pack;
@property (nonatomic, retain) MBProgressHUD *hud;



@end

@implementation PackDetailController


- (void)dealloc
{
    [_table release];
    [super dealloc];
}

- (instancetype) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)getMessage{
    NSNumber *idNum = [NSNumber numberWithInteger:_idNum];
    self.hud = [[MBProgressHUD alloc] init];
    [self.view addSubview:_hud];
    [_hud show:YES];
    _hud.labelText = @"正在拼命加载......";
    //    在网络请求之后
    
    __block MBProgressHUD *hud = _hud;
    NSDictionary *dic = @{@"client_id":@"qyer_ios", @"client_secret":@"cd254439208ab658ddf9", @"count": @"20", @"id":idNum, @"page":@"1", @"track_app_channel":@"App%2520Store", @"track_app_version":@"6.5.5", @"track_device_info":@"iPhone7%2C1", @"track_deviceid":@"216B2645-C28F-42CC-A74E-17449CA8DF1A", @"track_os":@"ios%25208.1.3", @"v":@"1"};
    [AsyncPostURLConnection asyncPostConncetWith:@"http://open.qyer.com/qyer/guide/detail" parmaters:dic message:^(NSData *data) {
        hud.hidden = YES;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSDictionary *author = [[dataDic objectForKey:@"authors"] firstObject];
        self.pack = [Pack packWithName:[dataDic objectForKey:@"cnname"] packInfo:[dataDic objectForKey:@"info"] authorName:[author objectForKey:@"username"] authorImage:[author objectForKey:@"avatar"] authorInfo:[author objectForKey:@"intro"]];
        [_table reloadData];
    }];
    
    
}


- (void)createtable{
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain] autorelease];
    _table.delegate = self;
    _table.dataSource = self;
    
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_table registerClass:[LabelCell class] forCellReuseIdentifier:@"label"];
    [_table registerClass:[MyTextCell class] forCellReuseIdentifier:@"mytext"];
    [self.view addSubview:_table];
    _table.nightBackgroundColor = UIColorFromRGB(0x343434);
    _table.nightSeparatorColor =  UIColorFromRGB(0x313131);
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
        return [LabelCell heghtForText:_pack.packInfo] + 30 * OffHeight;
    }
    if (indexPath.row == 5) {
        return [LabelCell heghtForText:_pack.authorInfo] + 20 * OffHeight;
    }
    return 30 * OffHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mytext"];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    if (indexPath.row == 0) {
        cell.myLabel.text = _pack.name;
    }
    if (indexPath.row == 1) {
        cell.myLabel.text = @"锦囊介绍";
    }
    if (indexPath.row == 2) {
        LabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label"];
        cell.label.text = _pack.packInfo;
        CGRect rect = [_pack.packInfo boundingRectWithSize:CGSizeMake(345 * OffWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
        [cell.label setFrame:CGRectMake(15 * OffWidth, 15 * OffHeight, 345 * OffWidth, rect.size.height * OffHeight)];
        return cell;
    }
    if (indexPath.row == 3) {
        cell.myLabel.text = @"锦囊作者";
    }
    if (indexPath.row == 4) {
        cell.textLabel.text = _pack.authorName;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:_pack.authorImage] placeholderImage:nil];
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 10 * OffHeight;
    }
    if (indexPath.row == 5) {
        LabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"label"];
        CGRect rect = [_pack.authorInfo boundingRectWithSize:CGSizeMake(345 * OffWidth, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
        [cell.label setFrame:CGRectMake(15 * OffWidth, 15 * OffHeight, 345 * OffWidth, rect.size.height * OffHeight)];
        cell.label.text = _pack.authorInfo;
        return cell;
    }

    return cell;
}










- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1];
    
    self.navigationItem.title = _pack.name;
    [self createtable];
    [self getMessage];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
